package com.clip.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.clip.entity.UserRegApply;
import com.clip.entity.UserRegInvites;
import com.clip.service.UserRegApplyService;
import com.clip.service.UserRegInvitesService;
import com.clip.util.Struts2Utils;
import com.clip.util.ToolsUtils;
import com.clip.util.mail.SimpleMailSender;
import com.opensymphony.xwork2.ActionSupport;

@Controller
@Scope("prototype")
public class UserRegApplyAction extends ActionSupport  {
	
	private static final long serialVersionUID = 4112873380897219581L;
	
	@Autowired
	private UserRegApplyService userRegApplyService;
	@Autowired
	private UserRegInvitesService userRegInvitesService;
	
	private String email;//
	private String name;//
	private String birthday;
	private String living;
	private String job;
	private String edu;
	private String blog;
	private String vcode;//
	private String sex;

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		return super.execute();
	}
	
	public void getApplyUser(){
		String json = "{'flag':0,'msg':'无效的邀请'}";
		UserRegInvites uri = userRegInvitesService.findUserRegInvitesByCode(vcode);
		if(uri!=null){
			if(uri.getStatus()==0){
				UserRegApply userRegApply = userRegApplyService.findUserRegApplyById(uri.getReguid());
				if(userRegApply!=null){
					json = "{" +
							"'flag':1," +
							"'msg':'成功'," +
							"'email':'"+userRegApply.getEmail()+"'," +
							"'name':'"+userRegApply.getName()+"'" +
							"}";
				}
			}
		}
		Struts2Utils.renderText(json);
	}
	
	public void applyUser(){
		HttpServletRequest request = ToolsUtils.getRequest();
		String verifyCode=(String)request.getSession().getAttribute("verifyCode");
		String emailRegex="^[_\\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\\.){1,4}[a-z]{2,3}";
		String nameRegex="^[A-Za-z0-9\u4e00-\u9fa5]{2,20}";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String json = "";
		int flag = 0;
		String msg = "";
		vcode = vcode.toLowerCase();
		verifyCode = verifyCode.toLowerCase();
		email = email.trim();
		name = name.trim();
		sex = sex!=null&&!sex.matches("^[0-9]+$") ? sex : "1"; 
		if(verifyCode.equals(vcode)){
			if(email.length()<=0){
				flag = 1;
				msg = "请输入邮箱地址";
			}
			if(name.length()<=0){
				flag = 1;
				msg = "请输入您的姓名";
			}
			if(flag!=1){
				if(!email.matches(emailRegex)){
					flag = 2;
					msg = "请输入正确的邮箱地址";
				}
				if(!name.matches(nameRegex)){
					flag = 2;
					msg = "请输入您的姓名";
				}
				if(flag!=2){
					UserRegApply userRegApply = new UserRegApply();
					if(birthday!=null&&birthday.length()>0){
						try {
							userRegApply.setBirth(sdf.parse(birthday).getTime());
						} catch (ParseException e) {
							
						}
					}
					userRegApply.setBlog(ToolsUtils.htmlEnCode(blog));
					userRegApply.setEdu(ToolsUtils.htmlEnCode(edu));
					userRegApply.setEmail(email);
					userRegApply.setJob(ToolsUtils.htmlEnCode(job));
					userRegApply.setLiving(ToolsUtils.htmlEnCode(living));
					userRegApply.setName(ToolsUtils.htmlEnCode(name));
					userRegApply.setSex(Integer.valueOf(sex));
					userRegApply.setStatus(0);
					
					if(userRegApplyService.save(userRegApply)>0){
						String title="档期注册申请";
						Map<String, Object> root = new HashMap<String, Object>();
						root.put("name", userRegApply.getName());
						String mailcontent = com.clip.util.FtlUtils.stringRender(root,"registerApply.ftl");
						SimpleMailSender.send(mailcontent,title, userRegApply.getEmail());
						flag = 3;
						msg = "您的申请已提交，申请结果会在稍后发送至您填写的邮箱内，请注意查收！";
					}else{
						msg = "提交失败,请尝试刷新页面重新提交";
					}
					
				}
			}
		}else{
			msg = "验证码错误";
		}
		json = "{'flag':"+flag+",'msg':'"+msg+"'}";
		Struts2Utils.renderText(json);
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getBirthday() {
		return birthday;
	}

	public void setBirthday(String birthday) {
		this.birthday = birthday;
	}

	public String getLiving() {
		return living;
	}

	public void setLiving(String living) {
		this.living = living;
	}

	public String getJob() {
		return job;
	}

	public void setJob(String job) {
		this.job = job;
	}

	public String getEdu() {
		return edu;
	}

	public void setEdu(String edu) {
		this.edu = edu;
	}

	public String getBlog() {
		return blog;
	}

	public void setBlog(String blog) {
		this.blog = blog;
	}

	public String getVcode() {
		return vcode;
	}

	public void setVcode(String vcode) {
		this.vcode = vcode;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}
	
}
